//
//  ClubManagerListViewController.swift
//  77Club
//
//  Created by Apple on 2024/12/27.
//

import UIKit
import JXSegmentedView

class ClubManagerListViewController: ClubBaseViewController {

    private var exploreLabel = UIIconButton(style: .leading(itemSpace: 1))
    private lazy var tableView: UITableView = {
        let tableView = UITableView()
        tableView.delegate = self
        tableView.dataSource = self
        tableView.backgroundColor = .clear
        tableView.showsVerticalScrollIndicator = false
        tableView.register(ClubForumManagerMemberCell.self)
        tableView.contentInsetAdjustmentBehavior = .never
        tableView.separatorStyle = .none
        tableView.rowHeight = 56.0.Scale
        return tableView
    }()
    private var empteView = ClubEmptyView()
    
    var pageIndex = 1
    var communityId = ""
    var dataSource = [ClubForumMemberModel]() {
        didSet{
            tableView.reloadData()
        }
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        loadData()
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()

        view.subviews{
            exploreLabel
            tableView
            empteView
        }
        
        exploreLabel.leading(16.0.Scale).top(4.0.Scale).height(14.0.Scale)
        tableView.fillContainer().top(22.0.Scale)
        empteView.leading(0).trailing(0).top(.navi_height + 150.0.Scale).height(200.0.Scale)
        empteView.titleString = "空空如也哟"
        empteView.isHidden = true
        exploreLabel.style { v in
            v.iconSize = CGSize(width: 14.0.Scale, height: 14.0.Scale)
            v.setImage("Forum_member_alert".toImage, for: .normal)
            v.setTitleColor("#969799".toColor, for: .normal)
            v.setTitle("最多添加5位管理员", for: .normal)
            v.titleLabel?.font = 10.font_medium
            v.contentHorizontalAlignment = .leading
            v.isUserInteractionEnabled = false
        }
        
        tableView.refresh_loadMore {
            self.loadMoreData()
        }.refresh_pull {
            self.loadData()
        }
    }
    
    private func loadData(){
        
        pageIndex = 1
        dataSource.removeAll()
        ToastUtil.show()
        Task{
            let res = await self.network.request(.squareMemberList(gid: self.communityId, role: "1", keyword: "", page: pageIndex), type: ClubForumMemberModel.self)
            tableView.refresh_end()
            ToastUtil.dismiss()
            if let models = res.models, res.isSuccess {
                self.dataSource = models
                empteView.isHidden = models.count > 0
                exploreLabel.isHidden = !(models.count > 0)
            }
        }
    }

    private func loadMoreData(){
        
        pageIndex += 1
        ToastUtil.show()
        Task{
            let res = await self.network.request(.squareMemberList(gid: self.communityId, role: "1", keyword: "", page: pageIndex), type: ClubForumMemberModel.self)
            tableView.refresh_end()
            ToastUtil.dismiss()
            if let models = res.models, res.isSuccess {
                var data = self.dataSource
                data.append(contentsOf: models)
                self.dataSource = data
            }
        }
    }
}

extension ClubManagerListViewController: UITableViewDelegate, UITableViewDataSource {

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        
        return dataSource.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        let cell = tableView.dequeueReusableCell(withIdentifier: "ClubForumManagerMemberCell", for: indexPath) as! ClubForumManagerMemberCell
        cell.model = dataSource[indexPath.row]
        cell.cancelManager = { user_id in
            Task{
                ToastUtil.show()
                let models = await self.network.request(.forumCancelAdmin(gid: self.communityId, user_id: user_id))
                if models.isSuccess {
                    "解除管理员成功".toast_success
//                    self.loadData()
                    DispatchQueue.main.async {
                        NotificationCenter.default.post(name: .updateForumMemberState, object: nil)
                    }
                }else{
                    models.errorMessage?.toast_error
                }
            }
        }
        return cell
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        
        let managerEditVC = ClubAdminSettingViewController()
        managerEditVC.communityId = self.communityId
        managerEditVC.user_id = dataSource[indexPath.row].user_id
        self.navigationController?.pushViewController(managerEditVC, animated: true)
    }
}



extension ClubManagerListViewController:JXSegmentedListContainerViewListDelegate {
    func listView() -> UIView {
        return view
    }
}
